#!/usr/bin/node

const https=require('https'),
      http=require('http'),
      fs=require('fs'),
      url=require('url'),
      log=console.log,
      cheerio=require('cheerio'),
      addr='https://maoyan.com/films?showType=3';
url.parse(addr).headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'};
const movieList=require('./data.js');


let moviesList=[]; 
for(let i=0;i<movieList.length;i++){
  if(movieList[i].movieRange!=='暂无评分'){
    moviesList.push(movieList[i]);
  }
}

for(let i=0;i<moviesList.length-1;i++){
  for(let j=0;j<moviesList.length-i-1;j++){
    if(Number(moviesList[j].movieRange)<Number(moviesList[j+1].movieRange)){
      let temp=moviesList[j];
      moviesList[j]=moviesList[j+1];
      moviesList[j+1]=temp;
    }
  }
}
http.createServer((req,res)=>{
  let html=fs.readFileSync('./index.html').toString('utf8');
  if(req.url==='/'){  
    res.end(html);
  }else if(req.url==='/getlist'){
    res.end(JSON.stringify(moviesList));
  }
}).listen(8080);

